import type { ${model.code}Item } from '@/services/${model.module}/types';
import { add${model.code}, update${model.code}, remove${model.code} } from '@/services/${model.module}/${model.code}';
import { message } from 'antd';

/**
 * 添加${model.name}
 *
 * @param fields
 */
export const handleAdd = async (fields: ${model.code}Item) => {
  const hide = message.loading('正在新增${model.name}');

  try {
    await add${model.code}({ ...fields });
    hide();
    message.success('${model.name}添加成功');
    return true;
  } catch (error) {
    hide();
    message.error('${model.name}添加失败，请重试！');
    return false;
  }
};

/**
 * 更新${model.name}
 *
 * @param fields
 * @param currentRow
 */
export const handleUpdate = async (fields: ${model.code}Item, currentRow?: ${model.code}Item) => {
  const hide = message.loading('正在更新${model.name}');

  try {
    await update${model.code}({
      ...currentRow,
      ...fields,
    });
    hide();
    message.success('${model.name}更新成功');
    return true;
  } catch (error) {
    hide();
    message.error('${model.name}更新失败，请重试');
    return false;
  }
};

/**
 * 删除${model.name}
 *
 * @param selectedRows
 */
export const handleRemove = async (selectedRows: ${model.code}Item[]) => {
  const hide = message.loading('正在删除${model.name}');
  if (!selectedRows) return true;

  try {
    await remove${model.code}(selectedRows.map((row) => row.id));
    hide();
    message.success('${model.name}删除成功，即将刷新');
    return true;
  } catch (error) {
    hide();
    message.error('${model.name}删除失败，请重试');
    return false;
  }
};
